package graph


func GenerateGraph(chart [][]int) Graph {
	graph := Graph{map[int]*Node{}, map[*Edge]struct{}{}}
	for _, v := range chart {
		weight := v[0]
		from := v[1]
		to   := v[2]
		if !Contains(graph.Nodes, from) {
			graph.Nodes[from] = &Node{Val:from}
		}
		if !Contains(graph.Nodes, to) {
			graph.Nodes[to] = &Node{Val:to}
		}
		fromN := graph.Nodes[from]
		toN   := graph.Nodes[to]
		NewEdge := &Edge{weight, fromN, toN}
		fromN.Out++
		fromN.Nexts = append(fromN.Nexts, toN)
		fromN.Edges = append(fromN.Edges, NewEdge)
		toN.In++
		graph.Edges[NewEdge] = struct{}{}
	}
	return graph
}

func Contains(set map[int]*Node, node int)  (ok bool) {
	_, ok = set[node]
	return
}

func Contains1(set map[*Node]struct{}, node *Node)  (ok bool) {
	_, ok = set[node]
	return
}